package com.liang.blog.mapper;

import com.liang.blog.model.BlogInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface BlogInfoMapper {
    /**
     * 查询所有博客
     */
    @Select("select b.id,b.title,b.content,b.user_id,b.update_time,b.kind,u.user_name from blog b,user u where u.id = b.user_id and\n" +
            "b.delete_flag = 0 order by update_time desc")
    List<BlogInfo> selectAll();
    /**
     * 根据博客ID,查询博客信息
     */
    @Select("select id,title,content,user_id,update_time,kind from blog where id = #{id} and delete_flag = 0 ")
    BlogInfo selectById(Integer id);
    /**
     * 插入博客
     */
    @Insert("insert into blog(title,content,user_id,kind) values(#{title},#{content},#{userId},#{kind})")
    Integer insertBlog(BlogInfo blogInfo);
    /**
     * 更新博客
     */
    Integer updateBlog(BlogInfo blogInfo);

    /**
     * 根据用户Id查询该用户发布的博文数量
     * @param id
     * @return
     */
    @Select("select count(*) from blog b, user u where u.id = b.user_id and b.delete_flag= 0 and u.id = #{id}")
    Long selectCountById(Integer id);

    @Select("select count(kind) from blog b,user u where u.id = b.user_id and b.delete_flag= 0 and u.id = #{id}")
    Long selectKindById(Integer id);



    List<BlogInfo> selectByTitleKey(String titleKey);
}
